/*
 * MAdministradorEscenarios.java
 *
 * Created on 27 de abril de 2007, 07:54 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ig.metrica.ui.escenario;

import ig.metrica.ejb.escenario.EscenariosSBbeanBean;
import ig.metrica.escenario.GenerarEscenariosUtil;
import ig.metrica.ui.interfaces.comunes.IModelo;
import ig.util.NoContinuaException;
import java.util.Collection;
import java.util.Hashtable;
import javax.swing.JOptionPane;

/**
 *
 * @author jsanchez
 */
public class MAdministradorEscenarios implements IModelo{
    AdminEscenarios panel=null;
    /** Creates a new instance of MAdministradorEscenarios */
    public MAdministradorEscenarios(AdminEscenarios panel) {
        this.panel=panel;
    }
    
    public void guardar() throws NoContinuaException {
    }
    
    public Collection buscar() throws NoContinuaException {
        try {
            return this.lookupEscenariosSBbeanBean().findAll();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new NoContinuaException(ex.toString());
        }
    }
    
    public void modificar() throws NoContinuaException {
    }
    
    public void nuevo() throws NoContinuaException {
    }
    
    public void eliminar() throws NoContinuaException {
        new EliminarEscenario(this.panel,this).start();
    }
    
    public void salir() throws NoContinuaException {
    }
    
    public void imprimir() throws NoContinuaException {
    }
    
    public Object getTO() throws NoContinuaException {
        return null;
    }
    
    public void cancelar() throws NoContinuaException {
    }
    
    EscenariosSBbeanBean escenariosSBbeanBean = new EscenariosSBbeanBean();
    public ig.metrica.ejb.escenario.EscenariosSBbeanBean lookupEscenariosSBbeanBean() {
        return escenariosSBbeanBean;
        /*try {
            Properties prop = new Properties();
            prop.put( Context.INITIAL_CONTEXT_FACTORY, IGBean.INITIAL_CONTEXT_FACTORY);
            String Sockec = new ConexionServer().getSocketServer();
            prop.put( Context.PROVIDER_URL, Sockec);
            
            javax.naming.Context c = new javax.naming.InitialContext(prop );
            Object remote = c.lookup("EscenariosSBbeanBean");
            ig.metrica.ejb.escenario.EscenariosSBbeanRemoteHome rv = (ig.metrica.ejb.escenario.EscenariosSBbeanRemoteHome) javax.rmi.PortableRemoteObject.narrow(remote, ig.metrica.ejb.escenario.EscenariosSBbeanRemoteHome.class);
            return rv.create();
            
        } catch(javax.naming.NamingException ne) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
            throw new RuntimeException(ne);
        } catch(javax.ejb.CreateException ce) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
            throw new RuntimeException(ce);
        } catch(java.rmi.RemoteException re) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,re);
            throw new RuntimeException(re);
        }*/
    }
    public void cargarDatos(){
        new BuscarDatos(this.panel,this).start();
    }
}


class BuscarDatos extends Thread{
    AdminEscenarios panel=null;
    MAdministradorEscenarios modelo=null;
    public BuscarDatos(AdminEscenarios panel,MAdministradorEscenarios modelo){
        this.panel=panel;
        this.modelo=modelo;
    }
    
    @Override
    public void run() {
        try {
            if(panel.getTablaSelecionada()!=null){
                String tablaSeleccionada=panel.getTablaSelecionada();
                Integer idEscenario=panel.getIdEscenario();
                if(tablaSeleccionada==null)
                    throw new Exception("Seleccione la tabla de la que desea ver datos");
                if(idEscenario==null)
                    throw new Exception("Seleccione el escenario del que desea ver datos");
                
                Hashtable ht=modelo.lookupEscenariosSBbeanBean().findData(tablaSeleccionada,idEscenario);
                
                try {Thread.sleep(1000);} catch (Exception e) {}
                String[] nombreCol=(String[]) ht.get("COLUMNAS");
                boolean[] edit=new boolean[nombreCol.length];

                for (int i = 0; i < edit.length; i++) {
                    edit[i]=false;
                }

                Object[][] data=(Object[][]) ht.get("DATOS");
                String[] tipos=(String[]) ht.get("TIPOS");
                panel.setDataTablas(data,nombreCol,edit,tipos);
            }
            panel.setSelectTabDatos();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(panel,ex.getMessage()+"\n["+ex.toString()+"]","Mensaje",JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
        
        this.panel.getPBarra().setIndeterminate(false);
        try {
            this.finalize();
        } catch (Throwable ex) {}        
    }
    
}

class EliminarEscenario extends Thread{
    AdminEscenarios panel=null;
    MAdministradorEscenarios modelo=null;
    Integer id=null;
    public EliminarEscenario(AdminEscenarios panel,MAdministradorEscenarios modelo){
        this.panel=panel;
        this.modelo=modelo;
        this.id=panel.getIdEscenario();
    }
    
    @Override
    public void run() {
        try {
            try {Thread.sleep(1000);} catch (Exception e) {}
            Object[] tablas=GenerarEscenariosUtil.reloadTablasGuardar().toArray();
            if(id!=null){
                for (int i = 0; i < tablas.length; i++) {
                    //System.out.println("delete from "+tablas[i]+" where idescenario="+id);
                    modelo.lookupEscenariosSBbeanBean().delete(id,tablas[i].toString());
                }
                modelo.lookupEscenariosSBbeanBean().delete(id);
            }
            panel.limpiarTabla();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(panel,ex.toString(),"Mensaje",JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }finally{
            panel.setEliminando(false);
            this.panel.getPBarra().setIndeterminate(false);
        }
        try {
            this.finalize();
        } catch (Throwable ex) {}        
    }
}
